//
//  ZYCSettingVersonDescriptionsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/26.
//

#import "ZYCSettingVersonDescriptionsViewController.h"
#import "ZYCVersonDescriptionsView.h"
#import "ZYCHistoryVersionViewController.h"
#import "NSString+BTExtension.h"
#import "ThirdMacro.h"

@interface ZYCSettingVersonDescriptionsViewController ()<ZYCVersonDescriptionsViewDelegate>

@property (nonatomic, strong) ZYCVersonDescriptionsView *descriptionsView;

@end

@implementation ZYCSettingVersonDescriptionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"版本说明";
    [self.view addSubview:self.descriptionsView];

    NSString *appName = [NSString appName];
    NSString *appVersion = [NSString appBuildVersion];

    self.descriptionsView.appNameLabel.text = appName;
    self.descriptionsView.appVersonLabel.text = [NSString stringWithFormat:@"Version V%@",appVersion];
 
}

- (ZYCVersonDescriptionsView *)descriptionsView {
    if (!_descriptionsView) {
        _descriptionsView = [[ZYCVersonDescriptionsView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
        _descriptionsView.delegate = self;
    }
    return _descriptionsView;
}

#pragma mark - ZYCVersonDescriptionsViewDelegate

- (void)versionDescriptionsViewDidHistoryVersion{
    ZYCHistoryVersionViewController *vc = [ZYCHistoryVersionViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)versionDescriptionViewDidUpdateVersion{
    
//    [MrjToast makeToast:@"当前是最新版本" makeView:self.view];
    [self checkVersionUpdate];
}

- (void)checkVersionUpdate{
    
    [[URLManager sharedURLManager] getUrlString:Common_Get_VersionManager requestService:RequestServiceWorkBench parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                NSString *currentVersion =  responseObject[@"body"][@"versionNumber"];
                currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
                NSString *locationVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@""];
                if ([currentVersion  integerValue] > [locationVersion integerValue]){
                    [self popAlertView];
                }else{
                    [MrjToast makeToast:@"当前是最新版本!" makeView:self.view];
                }
            }
        } failure:^(NSError * _Nonnull error) {

        }];
}

- (void)popAlertView{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"版本更新" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        [self exitApplication];
    }];

    [alert addAction:cancelAction];
    UIAlertAction * updateAction = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@", kAppId];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",urlStr]]];
    }];
    [alert addAction:updateAction];
    [self presentViewController:alert animated:YES
                     completion:nil];
}

- (void)exitApplication {
    [UIView animateWithDuration:0.3f animations:^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
